package jsyf.oa.dao.device;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.stereotype.Repository;

import java.util.List;

import jsyf.oa.entity.device.DeviceBrandEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * Created by jjx on 2018/3/8.
 */
@Repository
public class DeviceBrandDao extends BaseDao {

    public DeviceBrandDao() {
        super.iBaseEntity = new DeviceBrandEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {

        return JSON.toJavaObject(json, DeviceBrandEntity.class);
    }

    public List<JSONObject> getBrandByCompanyId(String companyId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT t1.ID as id,t1.BRAND_NAME as brandName from t_dvc_brand as t1 ");
        sb.append(" LEFT JOIN t_sys_company_2_brand as t2  ");
        sb.append(" ON t1.ID = t2.BRAND_ID ");
        sb.append(" where t2.COMPANY_ID = ? and t1.DEL_FLAG=0 ");
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString(), new String[]{companyId});
        return jsonList;
    }
    public List<JSONObject> getDeviceTypeList(String companyId,String brandId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT");
        sql.append(" t_dvc_device_type.ID AS id,");
        sql.append(" t_dvc_device_type.`NAME` as `name`");
        sql.append(" FROM t_dvc_model LEFT JOIN t_sys_company_2_brand ON t_sys_company_2_brand.BRAND_ID = t_dvc_model.BRAND_ID");
        sql.append(" LEFT JOIN t_dvc_device_type ON t_dvc_device_type.ID = t_dvc_model.DEVICE_TYPE_ID");
        sql.append(" WHERE 1=1 ");
        if(!StringUtils.isBlank(companyId)){
            sql.append(" and t_sys_company_2_brand.COMPANY_ID ='" + companyId + "'");
        }if(!StringUtils.isBlank(brandId)){
            sql.append(" AND t_sys_company_2_brand.BRAND_ID='" + brandId + "'");
        }
        sql.append(" AND t_dvc_model.DEL_FLAG=0 GROUP BY t_dvc_device_type.ID");
        return  super.queryForJsonList(sql.toString());
    }
    public List<JSONObject> getBrandList2() {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT");
        sql.append(" t_dvc_brand.ID as id,");
        sql.append(" t_dvc_brand.BRAND_NAME as `name`,");
        sql.append(" t_dvc_brand.QUALITY_COMPANY as qualityCompany,");
        sql.append(" t_dvc_brand.AUTHENTICBRANDS_GROUP as authenticbrandsGroup,");
        sql.append(" t_dvc_brand.FINANCE_COMPANY as financeCompany,");
        sql.append(" t_dvc_brand.MANUFACTURING_COMPANY as manufacturingCompanys");
        sql.append(" FROM t_dvc_brand where t_dvc_brand.DEL_FLAG=0");
        return  super.queryForJsonList(sql.toString());
    }
    public List<JSONObject> getBrandList(String condition,Object[] params, int requestPage, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT");
        sql.append(" t_dvc_brand.ID as id,");
        sql.append(" t_dvc_brand.BRAND_NAME as `name`,");
        sql.append(" t_dvc_brand.QUALITY_COMPANY as qualityCompany,");
        sql.append(" t_dvc_brand.AUTHENTICBRANDS_GROUP as authenticbrandsGroup,");
        sql.append(" t_dvc_brand.FINANCE_COMPANY as financeCompany,");
        sql.append(" t_dvc_brand.MANUFACTURING_COMPANY as manufacturingCompanys");
        sql.append(" FROM t_dvc_brand ");
        sql.append(" WHERE 1=1 and t_dvc_brand.DEL_FLAG=0");
        sql.append(condition);
        super.appendPageSql(sql, requestPage, pageSize);
        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(), params);
        return  jsonList;
    }
    public Integer getPageCount1(String condition, Object[] params) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT");
        sql.append(" count(DISTINCT(t_dvc_brand.ID)) AS count");
        sql.append(" FROM t_dvc_brand ");
        sql.append(" WHERE 1=1 and t_dvc_brand.DEL_FLAG=0");
        sql.append(condition);
        String count = super.queryForString(sql.toString(), params);
        return Integer.parseInt(count);
    }
}
